package com.RPGDice;

import com.RPGDice.util.Dices;
import com.RPGDice.util.Settings;
import com.criticalritrpg.*;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

public class SelectDicesActivity extends Activity implements OnClickListener {

	EditText etD4, etD6, etD8, etD10, etD12, etD20;

	Button returnButton;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.select_dices);

		etD4 = (EditText) findViewById(R.id.et_d4);
		etD6 = (EditText) findViewById(R.id.et_d6);
		etD8 = (EditText) findViewById(R.id.et_d8);
		etD10 = (EditText) findViewById(R.id.et_d10);
		etD12 = (EditText) findViewById(R.id.et_d12);
		etD20 = (EditText) findViewById(R.id.et_d20);

		returnButton = (Button) findViewById(R.id.return_button);

		returnButton.setOnClickListener(this);

		loadDiceValues();

	}

	public void loadDiceValues() {
		int[] values = Settings.getNumberOfDices();
		etD4.setText(String.valueOf(values[Dices.DICE_D4]));
		etD6.setText(String.valueOf(values[Dices.DICE_D6]));
		etD8.setText(String.valueOf(values[Dices.DICE_D8]));
		etD10.setText(String.valueOf(values[Dices.DICE_D10]));
		etD12.setText(String.valueOf(values[Dices.DICE_D12]));
		etD20.setText(String.valueOf(values[Dices.DICE_D20]));
	}

	private void saveDiceValues() {
		int d4, d6, d8, d10, d12, d20;
		d4 = getIntValue(etD4);
		d6 = getIntValue(etD6);
		d8 = getIntValue(etD8);
		d10 = getIntValue(etD10);
		d12 = getIntValue(etD12);
		d20 = getIntValue(etD20);
		Settings.setDiceValues(d4, d6, d8, d10, d12, d20);
	}
	
	private int getIntValue(EditText et){
		int value = 0;
		try {
			value = Integer.parseInt(et.getText().toString());
		} catch (NumberFormatException e) {
			value = -1;
		}
		
		return value;
		
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.return_button:
			saveDiceValues();
			finish();
			break;
		}

	}

}
